//
//  UIView+Common.m
//
//  Created by lanou3g on 14-3-4.
//  Copyright (c) 2014年 lanou3g. All rights reserved.
//

#import "UIView+Common.h"
#import <sys/utsname.h>


@implementation UIView (Utility)

- (CGFloat)left {
    return self.frame.origin.x;
}

//将图片移动到左边
- (void)setLeft:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}


- (CGFloat)top {
    return self.frame.origin.y;
}


//设置view的top的位置
- (void)setTop:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}


- (CGFloat)right {
    return self.left + self.width;
}


- (void)setRight:(CGFloat)right {
    if(right == self.right){
        return;
    }
    CGRect frame = self.frame;
    frame.origin.x = right - frame.size.width;
    self.frame = frame;
}


- (CGFloat)bottom {
    return self.top + self.height;
}


//设置view底部的位置
- (void)setBottom:(CGFloat)bottom {
    if(bottom == self.bottom){
        return;
    }
    
    CGRect frame = self.frame;
    frame.origin.y = bottom - frame.size.height;
    self.frame = frame;
}


- (CGFloat)centerX {
    return self.center.x;
}


- (void)setCenterX:(CGFloat)centerX {
    self.center = CGPointMake(centerX, self.center.y);
}


- (CGFloat)centerY {
    return self.center.y;
}


- (void)setCenterY:(CGFloat)centerY {
    self.center = CGPointMake(self.center.x, centerY);
}


- (CGFloat)width {
    return self.frame.size.width;
}


- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}


- (CGFloat)height {
    return self.frame.size.height;
}


- (void)setHeight:(CGFloat)height {
    if(height == self.height){
        return;
    }
    
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGPoint)origin {
    return self.frame.origin;
}


- (void)setOrigin:(CGPoint)origin {
    CGRect frame = self.frame;
    frame.origin = origin;
    self.frame = frame;
}


- (CGSize)size {
    return self.frame.size;
}


- (void)setSize:(CGSize)size {
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (id)subviewWithTag:(NSInteger)tag{
    for(UIView *view in [self subviews]){
        if(view.tag == tag){
            return view;
        }
    }
    return nil;
}

- (void)removeAllSubviews {
    while (self.subviews.count) {
        UIView* child = self.subviews.lastObject;
        [child removeFromSuperview];
    }
}


+ (instancetype)viewFromXib {
    NSBundle* bundle = [NSBundle bundleWithURL:[[NSBundle mainBundle] URLForResource:@"s" withExtension:@"bundle"]];
    NSString* className = NSStringFromClass([self class]);
    return [[bundle loadNibNamed:className owner:nil options:nil] objectAtIndexSafe:0];
}

- (void)frameCompatibleHeight:(CGRect)rect
{
    self.frame = rect;
    [self setHeight:ceil(self.height * ([UIScreen mainScreen].bounds.size.width / 320))];
    
}

-(CGRect)autoAdaptive{
    CGRect rect = self.frame;
    rect.size.height = rect.size.height*([UIScreen mainScreen].bounds.size.width/DefaultDeviceWidth);
    self.frame = rect;
    return rect;
}

- (void)frameCompatibleSize:(CGRect)rect
{
    self.frame = rect;
    
    NSInteger wid = (NSInteger)[UIScreen mainScreen].bounds.size.width;
    
    if (wid != 320) {
        CGFloat rate = [UIScreen mainScreen].bounds.size.width / 320;
        [self setSize:CGSizeMake(ceil(self.width * rate), ceil(self.height * rate))];
    }
    
}

+ (CGFloat)compatibleLength:(CGFloat)length {
    if (([UIScreen mainScreen].bounds.size.width) == 320) {
        return length;
    }
    return length / 320 * ([UIScreen mainScreen].bounds.size.width);
}

- (UIImage*) snapshotImage {
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef currnetContext = UIGraphicsGetCurrentContext();
    [self.layer renderInContext:currnetContext];
    UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
    // 使当前的context出堆栈
    UIGraphicsEndImageContext();
    return image;
}

- (UIImage *)snapshotWithScale:(CGFloat)scale
{
    return [self snapshotWithScale:scale andFrame:self.bounds];
}

- (UIImage *) snapshotWithScale:(CGFloat)scale andFrame:(CGRect) frame {
    UIImage *image = nil;
    UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [UIScreen mainScreen].scale);
    
    [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:NO];
    image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    CGSize newSize = CGSizeMake(frame.size.width * scale, frame.size.height * scale);
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

@end

